
const a_bannerId = ['adunit-ebac3d1aa2469841', 'adunit-4505ca0d79fae8df', 'adunit-29fbfdcb128fa92e'];
let n_index = 0;
let bannerAd;
let bannerAdHeight;
var Events = require('ym-Events')
module.exports = {
    init: function () {
        // CC_WECHATGAME && this.createBanner();
        this.lastHeight = 0;
        this.intervalId =null
    },

    createBanner(ad='adunit-4505ca0d79fae8df',x,y,time){
        if(!CC_WECHATGAME){return}
        this._bannerx = x;
        this._bannery = y;
        var self = this;
        var systemInfo = wx.getSystemInfoSync();
        var height = systemInfo.screenHeight * 0.35;
        var width = systemInfo.screenWidth;
        if (!!bannerAd) bannerAd.destroy();
        bannerAd = wx.createBannerAd({
            adUnitId: ad,
            style: { left: 0, top: 0, width: width, height: height }
        });
        bannerAd.onResize((res) => {
            if (!bannerAd) return;
            bannerAdHeight = res.height;
            Events.emit('banner_show',res);
            self.moveBanner(x,y)
        })
        bannerAd.onError((e) => { console.log('onError:', e); });
        bannerAd.show();
        if(time){
            if(this._updateBanner){
                clearTimeout(this._updateBanner);
            }
            this._updateBanner = setTimeout(function(){
                this.showBanner(this._bannerx,this._bannery,time)
            }.bind(this),time*1000)
        }
    },
    moveBanner(x,y){
        var systemInfo = wx.getSystemInfoSync();
        this._bannerx = x || 0;
        var th = "undefined" == typeof y ? bannerAdHeight : y * systemInfo.screenHeight / cc.winSize.height;
        this._bannery = th;
        cc.log("moveBanner:", this._bannerx, this._bannery);
        bannerAd.style.top = systemInfo.screenHeight - th;
        bannerAd.style.left = this._bannerx * systemInfo.screenWidth / cc.winSize.width;
    },
    destroy(){
        bannerAd && bannerAd.destroy();
        bannerAd = null;
        if(this._updateBanner){
            clearTimeout(this._updateBanner);
        }
        this._updateBanner = null
    },
    getBannerADHeight(){
        return bannerAdHeight
    },
    moveRevsert(){
        bannerAd.style.top = this.lastHeight
    }
}
